# WebSocket 模块 Makefile

# 变量定义
BINARY_NAME=websocket-server
BINARY_UNIX=$(BINARY_NAME)_unix
BUILD_DIR=build
EXAMPLES_DIR=examples

# Go 相关变量
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod

# 默认目标
.DEFAULT_GOAL := help

# 帮助信息
.PHONY: help
help: ## 显示帮助信息
	@echo "WebSocket 模块化库 - 可用命令:"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

# 依赖管理
.PHONY: deps
deps: ## 下载依赖
	$(GOMOD) download
	$(GOMOD) tidy

# 测试
.PHONY: test
test: ## 运行所有测试
	$(GOTEST) -v ./...

.PHONY: test-coverage
test-coverage: ## 运行测试并生成覆盖率报告
	$(GOTEST) -v -coverprofile=coverage.out ./...
	$(GOCMD) tool cover -html=coverage.out -o coverage.html
	@echo "覆盖率报告已生成: coverage.html"

# 构建
.PHONY: build
build: ## 构建服务器
	$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) ./examples/advanced_server.go

.PHONY: build-simple
build-simple: ## 构建简单服务器
	$(GOBUILD) -o $(BUILD_DIR)/simple-server ./examples/simple_server.go

.PHONY: build-client
build-client: ## 构建测试客户端
	$(GOBUILD) -o $(BUILD_DIR)/client-test ./examples/client_test.go

.PHONY: build-all
build-all: build build-simple build-client ## 构建所有目标

# 运行
.PHONY: run
run: build ## 运行高级服务器
	./$(BUILD_DIR)/$(BINARY_NAME)

.PHONY: run-simple
run-simple: build-simple ## 运行简单服务器
	./$(BUILD_DIR)/simple-server

.PHONY: run-client
run-client: build-client ## 运行测试客户端
	./$(BUILD_DIR)/client-test ws://127.0.0.1:8080/ws

# 清理
.PHONY: clean
clean: ## 清理构建文件
	$(GOCLEAN)
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

# 代码质量
.PHONY: fmt
fmt: ## 格式化代码
	$(GOCMD) fmt ./...

.PHONY: vet
vet: ## 代码静态检查
	$(GOCMD) vet ./...

.PHONY: lint
lint: fmt vet ## 代码质量检查

# 文档
.PHONY: docs
docs: ## 生成文档
	@echo "文档已生成:"
	@echo "  - README.md: 详细使用文档"
	@echo "  - MIGRATION.md: 迁移指南"
	@echo "  - SUMMARY.md: 项目总结"

# 示例
.PHONY: examples
examples: ## 运行示例
	@echo "可用的示例:"
	@echo "  - simple_server.go: 简单服务器示例"
	@echo "  - advanced_server.go: 高级服务器示例"
	@echo "  - client_test.go: 客户端测试工具"

# 开发工具
.PHONY: dev-setup
dev-setup: deps lint ## 开发环境设置

.PHONY: pre-commit
pre-commit: lint test ## 提交前检查

# 性能测试
.PHONY: bench
bench: ## 运行性能测试
	$(GOTEST) -bench=. -benchmem ./...

# 安装
.PHONY: install
install: build ## 安装到系统路径
	cp $(BUILD_DIR)/$(BINARY_NAME) /usr/local/bin/

# 发布
.PHONY: release
release: clean build-all ## 准备发布版本
	@echo "发布版本已准备完成"
	@ls -la $(BUILD_DIR)/

# 容器化
.PHONY: docker-build
docker-build: ## 构建Docker镜像
	docker build -t websocket-module .

.PHONY: docker-run
docker-run: ## 运行Docker容器
	docker run -p 8080:8080 websocket-module

# 监控
.PHONY: monitor
monitor: ## 启动监控
	@echo "启动监控工具..."
	@echo "服务器地址: http://127.0.0.1:8080"
	@echo "WebSocket地址: ws://127.0.0.1:8080/ws"
	@echo "API端点:"
	@echo "  - GET /statistics"
	@echo "  - GET /online/lists"

# 快速开始
.PHONY: quick-start
quick-start: build run ## 快速开始（构建并运行）

# 完整测试
.PHONY: full-test
full-test: lint test test-coverage bench ## 完整测试套件

# 创建构建目录
$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

# 确保构建目录存在
build: $(BUILD_DIR)
build-simple: $(BUILD_DIR)
build-client: $(BUILD_DIR) 